package com.yuyou.fn.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.ExamPaper;
import com.yuyou.fn.educational.entity.Measurement;
import com.yuyou.fn.educational.entity.StudentMeasureScore;
import com.yuyou.fn.educational.service.IExamPaperService;
import com.yuyou.fn.educational.service.IMeasurementService;
import com.yuyou.fn.educational.service.IStudentMeasureScoreService;
import com.yuyou.fn.educational.vo.*;
import com.yuyou.fn.platform.entity.SysUser;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by lzh on 2017/11/27.
 */
@Controller
@RequestMapping("/api/admin/measurement")
public class AdminMeasurementController extends ApiBaseController {

    @Resource
    private IMeasurementService measurementService;

    @Resource
    private IStudentMeasureScoreService studentMeasureScoreService;

    @Resource
    private IExamPaperService examPaperService;

    @ApiOperation("保存或者更新分数设置")
    @RequestMapping(value = "/saveOrUpdateMeasurement",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateMeasurement(@ModelAttribute MeasurementSaveOrUpdateVo measurementSaveOrUpdateVo,HttpServletResponse response)throws  IOException{
        Measurement measurement=measurementService.saveOrUpdate(measurementSaveOrUpdateVo);
        return ReturnResult.success(measurement.getMeasurementId());
    }

    @ApiOperation("单个测评分数详情")
    @RequestMapping(value = "/measurementDetail",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Measurement> measurementDetail(@RequestParam(value = "measurementId")Long measurementId,HttpServletResponse response)throws  IOException{
        Measurement measurement=measurementService.findById(measurementId);
        return ReturnResult.success(measurement);
    }

    @ApiOperation("更新白名单设置")
    @RequestMapping(value = "/updateWhiltelist",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> updateWhiltelist(@RequestParam(value = "measurementId")Long measurementId , String whitelistConfig ,HttpServletResponse response)throws  IOException{
        WhiltelistConfigVo whiltelistConfigVo = JSONObject.parseObject(whitelistConfig, WhiltelistConfigVo.class);
        measurementService.updateWhiltelist(measurementId,whiltelistConfigVo );
        return ReturnResult.success("OK");
    }



    @ApiOperation("删除")
    @RequestMapping(value = "/deleteMeasurement",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> deleteMeasurement(@RequestParam(value = "measurementIdStr")String measurementIdStr,HttpServletRequest request,HttpServletResponse response)throws  IOException{
        List<Long> measurementIds=Utils.toLongIds(measurementIdStr);
        if (measurementIds.size()>0){
            measurementService.deleteMeasurement(measurementIds);
        }
        return ReturnResult.success("ok");
    }


    @ApiOperation("测评设置列表")
    @RequestMapping(value = "/measurementList",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<Measurement>> measurementList(@RequestParam(value = "pageNo")int pageNo,@RequestParam(value = "pageSize")int pageSize,@ModelAttribute MeasurementQueryVo measurementQueryVo,HttpServletRequest request)throws  IOException{
        //List<Measurement> measurementList=measurementService.findMeasureByQuery(measurementQueryVo);
        Page<Measurement> measurementPage=measurementService.findMeasurePageByQuery(measurementQueryVo,pageNo,pageSize);
        return ReturnResult.success(measurementPage);
    }


    @ApiOperation("保存或者更新学生分数")
    @RequestMapping(value = "/saveOrUpdateStudentScore",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateStudentScore(@ModelAttribute StudentMeasureScoreSaveOrUpdateVo studentMeasureScoreSaveOrUpdateVo,HttpServletRequest request)throws  IOException{
        SysUser sysUser=this.getUser(request);
        StudentMeasureScore studentMeasureScore=studentMeasureScoreService.saveOrUpdate(studentMeasureScoreSaveOrUpdateVo,sysUser);
        return ReturnResult.success(studentMeasureScore.getStudentMeasureScoreId());
    }


    @ApiOperation("学生测评分数详情")
    @RequestMapping(value = "/studentScoreDetail",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<StudentMeasureScore> studentScoreDetail(@RequestParam(value = "studentMeasureScoreId")Long studentMeasureScoreId,HttpServletResponse response)throws  IOException{
        StudentMeasureScore studentMeasureScore=studentMeasureScoreService.findById(studentMeasureScoreId);
        return ReturnResult.success(studentMeasureScore);
    }

    @ApiOperation("学生测评分数列表")
    @RequestMapping(value = "/studentScoreList",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<StudentMeasureScore>> studentScoreList(@ModelAttribute StudentMeasureScoreQueryVo studentMeasureScoreQueryVo,HttpServletRequest request)throws  IOException{
        List<StudentMeasureScoreDetailVo> studentMeasureScoreList=studentMeasureScoreService.findStudentScoreDetailByQuery(studentMeasureScoreQueryVo);
        return ReturnResult.success(studentMeasureScoreList);
    }


    @ApiOperation("保存或更新测评试卷")
    @ApiImplicitParams({@ApiImplicitParam(value = "examPaperId" ,required = false),@ApiImplicitParam(value = "examPaperNo" ,required = false),@ApiImplicitParam(value = "title" ,required = false),@ApiImplicitParam(value = "totalScore" ,required = false),@ApiImplicitParam(value = "testTime" ,required = false),@ApiImplicitParam(value = "areaTeamId" ,required = false),@ApiImplicitParam(value = "gradeId" ,required = false),@ApiImplicitParam(value = "subjectId" ,required = false)})
    @RequestMapping(value = "/saveOrUpdateExamPaper",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateExamPaper(ExamPaperSaveOrUpdateVo examPaperSaveOrUpdateVo)throws  IOException{
        examPaperService.saveOrUpdateExamPaper(examPaperSaveOrUpdateVo);
        return ReturnResult.success("OK");
    }

    @ApiOperation("删除测评试卷")
    @RequestMapping(value = "/delExamPaper",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> delExamPaper(@RequestParam("examPaperId") Long examPaperId)throws  IOException{
        examPaperService.delExamPaper(examPaperId);
        return ReturnResult.success("OK");
    }

    @ApiOperation("测评试卷列表")
    @ApiImplicitParams({@ApiImplicitParam(value = "examPaperId" ,required = false),@ApiImplicitParam(value = "examPaperNo" ,required = false),@ApiImplicitParam(value = "title" ,required = false),@ApiImplicitParam(value = "totalScore" ,required = false),@ApiImplicitParam(value = "testTime" ,required = false),@ApiImplicitParam(value = "areaTeamId" ,required = false),@ApiImplicitParam(value = "gradeId" ,required = false),@ApiImplicitParam(value = "subjectId" ,required = false)})
    @RequestMapping(value = "/examPapers",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> examPapers(ExamPaperQueryVo examPaperQueryVo ,@RequestParam(value = "pageNo" , defaultValue = "1")int pageNo,@RequestParam(value = "pageSize" ,defaultValue = "20")int pageSize)throws  IOException{
        Page<ExamPaper> page = examPaperService.findExamPapersByQuery(examPaperQueryVo ,pageNo,pageSize );
        return ReturnResult.success(page);
    }

    @ApiOperation("保存测评题目")
    @RequestMapping(value = "/saveOrUpdateExamPaperQuestions",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateExamPaperQuestions(@RequestBody ExamQuestionsSaveOrUpdateVo examQuestionsSaveOrUpdateVo )throws  IOException{
        examPaperService.saveOrUpdateExamPaperQuestions(examQuestionsSaveOrUpdateVo);
        return ReturnResult.success("OK");
    }

    @ApiOperation("试卷测评题目")
    @RequestMapping(value = "/questionsOfExamPaper",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<ExamPaperQuestionsVo> questionsOfExamPaper(@RequestParam("examPaperId") Long examPaperId)throws  IOException{
        ExamPaperQuestionsVo examPaperQuestionsVo = examPaperService.findQuestionsOfExamPaper(examPaperId);
        return ReturnResult.success(examPaperQuestionsVo);
    }

    @ApiOperation("测评绑定试卷")
    @RequestMapping(value = "/bindExams",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> bindExams(@RequestParam("measurementId") Long measurementId , @RequestParam(value = "examPaperIds[]" , required = false) List<Long> examPaperIds)throws  IOException{

        ExamConfigVo examConfigVo = new ExamConfigVo();
        if(examPaperIds !=  null ){
            List<ExamPaper> examPapers = examPaperService.findByIds(examPaperIds);
            for(ExamPaper examPaper : examPapers ){
                ExamConfigVo.ExamPaper exam = new ExamConfigVo.ExamPaper();
                exam.setExamPaperId(examPaper.getExamPaperId());
                exam.setExamPaperNo(examPaper.getExamPaperNo());
                examConfigVo.add(exam);
            }
        }

        Measurement measurement = new Measurement();
        measurement.setMeasurementId(measurementId);
        measurement.setExamConfig(JSONObject.toJSONString(examConfigVo, SerializerFeature.WriteNonStringKeyAsString));
        measurementService.updateSelectiveById(measurement);
        return ReturnResult.success("OK");
    }





}
